import matplotlib.pyplot as plt

# 读取数据
def read_data(filename):
    x, interpolated, actual = [], [], []
    with open(filename, 'r') as file:
        for line in file:
            parts = line.strip().split()
            x.append(float(parts[0]))
            interpolated.append(float(parts[1]))
            actual.append(float(parts[2]))
    return x, interpolated, actual

# 绘制函数
def plot_functions(x, interpolated, legend_label):
    plt.plot(x, interpolated, label=legend_label, linestyle='--' )
    

# 主程序
if __name__ == "__main__":
    n_values = [5, 10, 15, 20]
    plt.figure(figsize=(10, 6))

    for n in n_values:
        filename = f"./data/dataC_n{n}.txt"
        x, interpolated, actual = read_data(filename)
        plot_functions(x, interpolated, f"Interpolated n={n}")
    plt.plot(x, actual, label='Actual', linestyle='-')

    plt.title('Chebyshev Interpolation vs Actual Function')
    plt.xlabel('x')
    plt.ylabel('f(x)')
    plt.legend()
    plt.grid(True)
    plt.savefig("./picture/2C.png")
    plt.close()
    print("Plot saved to 2C.png")